package com.hfzy.ihk.web.aliyuncall.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by YANFA on 2018/9/11.
 */
@Configuration
public class TaskExecutorConfig {



    /**
     * 外呼线程池
     * @return
     */
    @Bean("aliyunCallTaskExecutor")
    public Executor aliyunCallTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//默认10个线程，
        executor.setMaxPoolSize(20);  //队列满了再加10个
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("aliyunCallTaskExecutor-");
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        return executor;
    }


    /**
     * 推送到ES线程池
     * @return
     */
    @Bean
    public Executor pushDataToEsTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//默认10个线程，
        executor.setMaxPoolSize(20); //队列满了再加10个
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("pushDataToEsTaskExecutor-");

        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        return executor;
    }

    /**
     * 推送到分析服务线程池
     * @return
     */
    @Bean
    public Executor pushDataToAnalysisTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//默认10个线程，
        executor.setMaxPoolSize(20); //队列满了再加10个
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("pushDataToAnalysisTaskExecutor-");
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        return executor;
    }


    /**
     * 收到识别以后推送到前端线程池，各线程池互补影响防止某一个服务雪崩阻塞其他服务
     * @return
     */
    @Bean
    public Executor pushASRDataExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//默认10个线程，
        executor.setMaxPoolSize(20); //队列满了再加10个
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("pushASRDataExecutor-");
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        return executor;
    }




    /**
     * 将数据推送到保利线程池
     * @return
     */
    @Bean
    public Executor pushDataToBlTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//默认10个线程，
        executor.setMaxPoolSize(20); //队列满了再加10个
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("pushDataToBlTaskExecutor-");
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，抛异常
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy() );
       // executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        return executor;
    }
}
